﻿@model IEnumerable<Web.Models.TagDto>

@{
    ViewBag.Title = "Monitor Tag States";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Select a Tag To View Data From</h2>

@if (Model.Any())
{
    <table class="ui-grid">
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Snapshot</th>
            <th>Action</th>
        </tr>
        @foreach (var tag in Model)
        {
            <tr>
                <td>@tag.Id</td>
                <td class="ui-column-wide">@tag.Name</td>
                <td> <div class="tag-live-data" data-tag-id="@tag.Guid.ToString().ToLower()">...</div> </td>
                <th>@Html.ActionLink("Graph", "Graph", "Monitoring", new {id = tag.Id}, null)</th>
            </tr>
        }
    </table>
}
else
{
    <label>No tag is defined.</label>
}

@section scripts
{
    <script type="text/javascript">
        $(document).ready(function() {
            $.connection.hub.logging = true;
            var hub = $.connection.broadcastHub;
            function connectionReady() {
                console.log("connected to SingalR");
            }

            hub.client.updateDouble = function (tagId, times, value) {
                var obj = $(".tag-live-data[data-tag-id=" + tagId + "]");
                var owner = obj[0];
                if (!owner.tagData) {
                    owner.tagData = [];
                }
                owner.tagData.push(value);
                if (owner.tagData.length > 25) {
                    owner.tagData.splice(0, 1);
                }
                obj.sparkline(owner.tagData);
            };

            $.connection.hub.error(function() {
                alert("Error connecting to the server");
            });

            $.connection.hub.start()
                .done(connectionReady)
                .fail(function () { console.log("Could not Connect!"); });
        })
    </script>
}
